#ifndef _HIGERCC1101_H
#define _HIGERCC1101_H
#include "CC1101.h"

#include "stm32f10x_exti.h"
#include "stm32f10x.h"

#include "cominicate.h"

#ifdef CC_BASE
#include "nvic.h"
#else
#include "mpu430.h"
#include "port.h"
#endif

/* bit value */
#ifndef BV
#define BV(n)      (1 << (n))
#endif

/* GDO functionality */
#define MRFI_GDO_SYNC           6
#define MRFI_GDO_CCA            9
#define MRFI_GDO_PA_PD          27  /* low when transmit is active, low during sleep */
#define MRFI_GDO_LNA_PD         28  /* low when receive is active, low during sleep */

#define MRFI_CONFIG_GDO0_AS_PAPD_SIGNAL()  halSpiWriteReg(CCxxx0_IOCFG0, MRFI_GDO_PA_PD )
#define MRFI_CONFIG_GDO0_AS_SYNC_SIGNAL()  halSpiWriteReg(CCxxx0_IOCFG0, MRFI_GDO_SYNC)

#define MRFI_GDO0_PIN_IS_HIGH()            GPIO_ReadInputDataBit(GD0IRQ_GPIO, GD0IRQ)
#define MRFI_ENABLE_GDO0_INT()             NVIC_CC1101GD0(ENABLE)
#define MRFI_DISABLE_GDO0_INT()            NVIC_CC1101GD0(DISABLE)
#define MRFI_GDO0_INT_IS_ENABLED()         Is_CCxxx0_IRQ_enabled()
#define MRFI_GDO0_INT_FLAG_IS_SET()        ( EXTI_GetITStatus(GD0IRQ_EXTI) != RESET )
#define MRFI_CONFIG_GDO0_FALLING_EDGE_INT()
#define MRFI_CLEAR_GD0_PIN_INT_FLAG()      EXTI_ClearITPendingBit(GD0IRQ_EXTI)

#define MRFI_SYNC_PIN_IS_HIGH()                      MRFI_GDO0_PIN_IS_HIGH()
#define MRFI_ENABLE_SYNC_PIN_INT()                   MRFI_ENABLE_GDO0_INT()
//#define MRFI_ENABLE_SYNC_PIN_INT()                  ( INFIX( P, __mrfi_GDO0_PORT__, IE )  |= ( ( rx_isr_context == true ) ? 0 : BV(__mrfi_GDO0_BIT__) ) )
#define MRFI_DISABLE_SYNC_PIN_INT()                  MRFI_DISABLE_GDO0_INT()
//#define MRFI_DISABLE_SYNC_PIN_INT()                 ( INFIX( P, __mrfi_GDO0_PORT__, IE )  &= ~( ( rx_isr_context == true ) ? 0 : BV(__mrfi_GDO0_BIT__) ) )
#define MRFI_SYNC_PIN_INT_IS_ENABLED()               MRFI_GDO0_INT_IS_ENABLED()
#define MRFI_CLEAR_SYNC_PIN_INT_FLAG()               MRFI_CLEAR_GD0_PIN_INT_FLAG()
#define MRFI_SYNC_PIN_INT_FLAG_IS_SET()              MRFI_GDO0_INT_FLAG_IS_SET()
#define MRFI_CONFIG_SYNC_PIN_FALLING_EDGE_INT()      MRFI_CONFIG_GDO0_FALLING_EDGE_INT()

#define MRFI_PAPD_PIN_IS_HIGH()                      MRFI_SYNC_PIN_IS_HIGH()
#define MRFI_CLEAR_PAPD_PIN_INT_FLAG()               MRFI_CLEAR_SYNC_PIN_INT_FLAG()
#define MRFI_PAPD_INT_FLAG_IS_SET()                  MRFI_SYNC_PIN_INT_FLAG_IS_SET()
#define MRFI_CONFIG_PAPD_FALLING_EDGE_INT()          MRFI_CONFIG_SYNC_PIN_FALLING_EDGE_INT()
/* Exported macro ------------------------------------------------------------*/

#define RX433FLAG_RXBUSY 0X01
#define RX433FLAG_TXBUSY 0X02

#define MRFI_LENGTH_FIELD_SIZE  1

#define MRFI_PKTSTATUS_CCA BV(4)
#define MRFI_PKTSTATUS_CS  BV(6)

#define CCxxx0_READ_BIT 0x80
#define CCxxx0_BURST_BIT 0x40

/* transmit type parameter for MRFI_Transmit */
#define MRFI_TX_TYPE_FORCED           0
#define MRFI_TX_TYPE_CCA              1

#define MRFI_RX_METRICS_CRC_OK_MASK 0x80

#define MRFI_CCA_RETRIES 4


#define MRFI_STROBE_IDLE_AND_WAIT()                   \
{                                                     \
    halSpiStrobe( CCxxx0_SIDLE );                     \
    while (halSpiStrobe( CCxxx0_SNOP ) & 0xF0) ;      \
    }
/*********simplicitTi  end ************/

		
uint8_t MRFI_Transmit(uint8_t *packet, uint8_t txType);
void MRFI_GpioIsr(void);
void Mrfi_RxModeOn(void);

#endif
